<?

class gearman
{
	private static $handle;
	private static $worker;

	/**
	 * @return GearmanClient
	 */
	public static function client()
	{
		if ( !self::$handle )
		{
			$config = config::get('gearman');

			self::$handle = new GearmanClient();
			self::$handle->addServer( $config['host'], $config['port'] );
		}

		return self::$handle;
	}
	
	public static function worker()
	{
		if ( !self::$worker )
		{
			$config = config::get('gearman');

			self::$worker = new GearmanWorker();
			self::$worker->addServer( $config['host'], $config['port'] );
		}

		return self::$worker;
	}

	public static function do_background( $task, $data, $important = false )
	{
		if ( config::get('debug') )
			error_log('Background task ' . $task . ' added: ' . print_r($data, 1));

		if ( $important )
			self::client()->doHighBackground( $task, serialize($data) );
		else
			self::client()->doBackground( $task, serialize($data) );
	}
}